package com.jflm.service.impl;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jflm.domain.Allocation;
import com.jflm.domain.Consume;
import com.jflm.domain.Permission;
import com.jflm.domain.RuleSet;
import com.jflm.domain.Shop;
import com.jflm.domain.ShopMoney;
import com.jflm.domain.ShopRule;
import com.jflm.domain.ShopService;
import com.jflm.domain.User;
import com.jflm.mapper.RuleSetMapper;
import com.jflm.mapper.ShopMapper;
import com.jflm.mapper.ShopMoneyMapper;
import com.jflm.mapper.ShopServiceMapper;
import com.jflm.service.IShopService;

@Service("shopService")
public class ShopServiceImpl implements IShopService {
	private static final Logger LOG = LoggerFactory.getLogger(ShopServiceImpl.class);
	@Autowired
	private ShopMapper shopMapper;

	@Autowired
	private ShopMoneyMapper shopMoneyMapper;

	@Autowired
	private ShopServiceMapper shopServiceMapper;

	@Autowired
	private RuleSetMapper ruleSetMapper;

	@Override
	public List<Shop> findList(Shop shop) throws Exception {
		// TODO Auto-generated method stub
		return shopMapper.findList(shop);
	}

	@Override
	public int add(Shop shop) throws Exception {
		Date date = new Date();
		Timestamp nowdate = new Timestamp(date.getTime());
		// 向shopMoney表插入数据
		ShopMoney money = new ShopMoney();
		String moneyId = UUID.randomUUID().toString().replaceAll("-", "");
		money.setId(moneyId);
		money.setShopId(shop.getId());
		money.setMoney(shop.getMoney());
		// money.setType(shop.getType());
		money.setCreateTime(nowdate);
		money.setCreatorId(shop.getName());
		shopMapper.add3(money);
		// 向User表中插入数据
		User user = new User();
		String userId = UUID.randomUUID().toString().replaceAll("-", "");
		user.setName(shop.getUsername());
		user.setPwd(shop.getPwd());
		user.setId(userId);
		user.setType(shop.getType());
		user.setCreateTime(nowdate);
		user.setShopId(shop.getId());
		user.setRoleName("超级管理员");
		user.setPhone(shop.getPhone());
		shopMapper.add4(user);

		if (!StringUtils.isEmpty(shop.getService_span())) {
			if (!StringUtils.isEmpty(shop.getRuleSet_span())) {
				// 设置店铺分配的规则
				ShopService shopService = new ShopService();
				shopService.setId(UUID.randomUUID().toString().replaceAll("-", ""));
				shopService.setShopId(shop.getId());
				shopService.setCreateTime(nowdate);
				shopService.setDel(0);
				shopService.setServiceId(shop.getService_span());
				shopService.setRuleSetId(shop.getRuleSet_span());
				shopMapper.add2(shopService);
			}
		}
		if (!StringUtils.isEmpty(shop.getService_span2())) {

			if (!StringUtils.isEmpty(shop.getRuleSet_span2())) {
				// 设置店铺分配的规则
				ShopService shopService = new ShopService();
				shopService.setId(UUID.randomUUID().toString().replaceAll("-", ""));
				shopService.setCreateTime(nowdate);
				shopService.setShopId(shop.getId());
				shopService.setDel(0);
				shopService.setServiceId(shop.getService_span2());
				shopService.setRuleSetId(shop.getRuleSet_span2());
				shopMapper.add2(shopService);
			}
		}
		
		// 添加权限开始
		Permission permission = new Permission();
		permission.setDel(0);
		permission.setCreateTime(nowdate);
		permission.setUserId(userId);
		String pId = UUID.randomUUID().toString().replaceAll("-", "");
		permission.setId(pId);
		permission.setType(0);
		permission.setPermissionId(2);
		shopMapper.add5(permission);
		
		pId = UUID.randomUUID().toString().replaceAll("-", "");
		permission.setId(pId);
		permission.setType(0);
		permission.setPermissionId(1);
		shopMapper.add5(permission);

		pId = UUID.randomUUID().toString().replaceAll("-", "");
		permission.setId(pId);
		permission.setType(1);
		permission.setPermissionId(1);
		shopMapper.add5(permission);

		pId = UUID.randomUUID().toString().replaceAll("-", "");
		permission.setId(pId);
		permission.setType(1);
		permission.setPermissionId(2);
		shopMapper.add5(permission);
		// 添加权限结束

		RuleSet ruleSet = new RuleSet();
		ruleSet.setId(shop.getSetId());
		ruleSet = ruleSetMapper.findOne(ruleSet);
		shop.setSetName(ruleSet.getName());

		// 添加产品价格配置
		Allocation allocation = new Allocation();
		String uuid = UUID.randomUUID().toString().replaceAll("-", "");
		List<Shop> list = shopServiceMapper.searchPrice(shop);
		for (int i = 0; i < 7; i++) {
			if (i == 0) {
				allocation.setShopId(shop.getId());
				allocation.setDel(0);
				allocation.setCreateTime(nowdate);
				allocation.setId(uuid + i);
				allocation.setType("0");
				allocation.setName("反欺诈");
				allocation.setPrice(list.get(4).getDict_value());
				int flag = shopServiceMapper.addAllocation(allocation);
				LOG.info("#addAllocation()产品价格配置" + flag);
			}
			if (i == 1) {
				Allocation allocationPfk = new Allocation();
				allocationPfk.setShopId(shop.getId());
				allocationPfk.setId(uuid + i);
				allocationPfk.setDel(0);
				allocationPfk.setCreateTime(nowdate);
				allocationPfk.setShopId(allocation.getShopId());
				allocationPfk.setPrice(allocation.getPfk());
				allocationPfk.setType("1");
				allocationPfk.setName("评分卡");
				allocationPfk.setPrice(list.get(5).getDict_value());
				int flag = shopServiceMapper.addAllocation(allocationPfk);
				LOG.info("#addAllocation()产品价格配置" + flag);
			}
			if (i == 2) {
				Allocation allocationChsi = new Allocation();
				allocationChsi.setShopId(shop.getId());
				allocationChsi.setId(uuid + i);
				allocationChsi.setDel(0);
				allocationChsi.setCreateTime(nowdate);
				allocationChsi.setShopId(allocation.getShopId());
				allocationChsi.setPrice(allocation.getChsi());
				allocationChsi.setType("2");
				allocationChsi.setName("学信网");
				allocationChsi.setPrice(list.get(3).getDict_value());
				int flag = shopServiceMapper.addAllocation(allocationChsi);
				LOG.info("#addAllocation()产品价格配置" + flag);
			}
			if (i == 3) {
				Allocation allocationGjj = new Allocation();
				allocationGjj.setShopId(shop.getId());
				allocationGjj.setId(uuid + i);
				allocationGjj.setDel(0);
				allocationGjj.setCreateTime(nowdate);
				allocationGjj.setShopId(allocation.getShopId());
				allocationGjj.setPrice(allocation.getGjj());
				allocationGjj.setType("3");
				allocationGjj.setName("公积金");
				allocationGjj.setPrice(list.get(1).getDict_value());
				int flag = shopServiceMapper.addAllocation(allocationGjj);
				LOG.info("#addAllocation()产品价格配置" + flag);
			}
			if (i == 4) {
				Allocation allocationShe = new Allocation();
				allocationShe.setShopId(shop.getId());
				allocationShe.setId(uuid + i);
				allocationShe.setDel(0);
				allocationShe.setCreateTime(nowdate);
				allocationShe.setShopId(allocation.getShopId());
				allocationShe.setPrice(allocation.getSheb());
				allocationShe.setType("4");
				allocationShe.setName("社保");
				allocationShe.setPrice(list.get(0).getDict_value());
				int flag = shopServiceMapper.addAllocation(allocationShe);
				LOG.info("#addAllocation()产品价格配置" + flag);
			}
			if (i == 5) {
				Allocation allocationYys = new Allocation();
				allocationYys.setShopId(shop.getId());
				allocationYys.setId(uuid + i);
				allocationYys.setDel(0);
				allocationYys.setCreateTime(nowdate);
				allocationYys.setShopId(allocation.getShopId());
				allocationYys.setPrice(allocation.getYys());
				allocationYys.setType("5");
				allocationYys.setName("运营商");
				allocationYys.setPrice(list.get(2).getDict_value());
				int flag = shopServiceMapper.addAllocation(allocationYys);
				LOG.info("#addAllocation()产品价格配置" + flag);
			}
			if (i == 6) {
				Allocation allocationNut = new Allocation();
				allocationNut.setShopId(shop.getId());
				allocationNut.setId(uuid + i);
				allocationNut.setDel(0);
				allocationNut.setCreateTime(nowdate);
				allocationNut.setShopId(allocation.getShopId());
				allocationNut.setPrice(allocation.getBank());
				allocationNut.setType("6");
				allocationNut.setName("银行卡");
				allocationNut.setPrice(list.get(6).getDict_value());
				int flag = shopServiceMapper.addAllocation(allocationNut);
				LOG.info("#addAllocation()产品价格配置" + flag);
			}
		}
		
		//添加规则集
		List<ShopRule> list2 = shopServiceMapper.findData(shop.getRuleSetId());
		for (int j = 0; j < list2.size(); j++) {
			String uuid2 = UUID.randomUUID().toString().replaceAll("-", "");
			ShopRule shopRule2 = new ShopRule();
			shopRule2.setId(uuid2);
			shopRule2.setDel(0);
			shopRule2.setCreateTime(nowdate);
			shopRule2.setUpdateTime(nowdate);
			shopRule2.setUpdateUser("admin");
			shopRule2.setShopId(shop.getId());
			shopRule2.setDataId(list2.get(j).getDataId());
			shopRule2.setRuleCode(list2.get(j).getRuleCode());
			shopRule2.setDescription(list2.get(j).getDescription());
			shopRule2.setType(list2.get(j).getType());
			shopRule2.setSection(list2.get(j).getSection());
			shopRule2.setStore(list2.get(j).getStore());
			shopServiceMapper.datasave(shopRule2);
		}
		return shopMapper.add(shop);

	}

	@Override
	public List<ShopMoney> detailpage(ShopMoney shopMoney) throws Exception {
		// TODO Auto-generated method stub
		return shopMoneyMapper.detailpage(shopMoney);
	}

	// 产品配置
	@Override
	public int addAllocation(Allocation allocation) throws Exception {
		return shopServiceMapper.addAllocation(allocation);
	}

	// 消费记录
	@Override
	public List<Consume> consumeDetail(Consume consume) throws Exception {
		return shopMoneyMapper.consumeDetail(consume);
	}

	// 充值记录
	@Override
	public List<ShopService> detailpages(ShopService service) throws Exception {
		// TODO Auto-generated method stub
		return shopServiceMapper.detailpages(service);
	}

	@Override
	public List<Shop> findOne(Shop shop) throws Exception {
		return shopServiceMapper.findOne(shop);
	}

	@Override
	public int update(Shop shop) throws Exception {
		return shopServiceMapper.update(shop);
	}

	@Override
	public List<ShopRule> findRuleList(ShopRule shopRule) throws Exception {
		shopRule.setShopId(shopRule.getId());
		return shopServiceMapper.findRuleList(shopRule);
	}

	@Override
	public int delete1(ShopRule shopRule) throws Exception {
		// TODO Auto-generated method stub
		return shopServiceMapper.delete1(shopRule);
	}

	@Override
	public List<ShopRule> findDataList(ShopRule shopRule) throws Exception {
		// TODO Auto-generated method stub
		return shopServiceMapper.findDataList(shopRule);
	}

	@Override
	public int datasave(ShopRule shopRule2) throws Exception {
		// TODO Auto-generated method stub
		return shopServiceMapper.datasave(shopRule2);

	}

	@Override
	public List<Shop> searchPrice(Shop shop) throws Exception {
		return shopServiceMapper.searchPrice(shop);
	}

	@Override
	public List<Shop> searchShopId(Shop shop) throws Exception {
		// TODO Auto-generated method stub
		return shopServiceMapper.searchShopId(shop);
	}

	@Override
	public int updateAllocation(Shop shop) throws Exception {
		// TODO Auto-generated method stub
		return shopServiceMapper.updateAllocation(shop);
	}

	@Override
	public int updateRule(ShopRule shopRule) throws Exception {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public List<ShopRule> findRule(ShopRule shopRule) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ShopRule findStore(String id) throws Exception {
		// TODO Auto-generated method stub
		return shopServiceMapper.findStore(id);
	}

	@Override
	public int updateStore(ShopRule shopRule) throws Exception {
		// TODO Auto-generated method stub
		return shopServiceMapper.updateStore(shopRule);
	}
}
